import O1HD, {modules, services} from "../../engines/gameEngine";
import gameConfig from "../../engines/configs/gameConfig";
import {REWARD_TYPE} from "../../engines/services/userContent/userInterface";
import data_control from "../../engines/libs/data_control";
import mainRoot from "../../Game/MainRoot";
import UnLockPage from "../../Game/Page/UnLockPage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class power extends cc.Component {
    @property(cc.Label)
    lbPower: cc.Label = null;

    @property(cc.Label)
    lbResetPower: cc.Label = null;

    @property(cc.Prefab)
    InfinityPower: cc.Prefab = null;

    private readonly m_MemberTimeKey: string = "user-membertime";

    protected start() {
        this.onUpdateTimer()
        this.schedule(this.onUpdateTimer.bind(this), 1);
    }

    onUpdateTimer() {
        const currPower = O1HD.getServices().userSrv.getPower();
        const maxPower = O1HD.getServices().userSrv.getMaxPower();
        let resetTime = services().userSrv.getPowerResetTimer();
        resetTime = Math.max(0, resetTime);
        // console.info("currPower",currPower,maxPower)
        //体力已满
        if (currPower >= maxPower) {
            this.lbPower.string = `${currPower}`;
            //
            this.lbResetPower.string = ""
        } else {//体力还有得恢复
           
            this.lbPower.string = `${currPower}/${maxPower}`;
            //
            this.lbResetPower.string = data_control.getTimeFormatClock(resetTime);
            //恢复
            services().userSrv.onPowerTimerReset();
        }

        //判断无限体力是否过期
        let dateNow = data_control.getCurrDateDayString();
        let dateOld = O1HD.getServices().userSrv.getInfinityTime();
        if (dateOld) {
            if (dateOld != dateNow)//如果大于24小时
            {
                if (O1HD.getServices().userSrv.getInfinityPower()) {
                    O1HD.getServices().userSrv.setPower(gameConfig.MAX_POWER, false, true)
                }
                O1HD.getServices().userSrv.setInfinity(0);
                // O1HD.getModules().uiControlModule.showInfinityPower(gameConfig.FIRST_PLAY_INFINITY_PROBABILITY, mainRoot.UIRoot);
            }
        } else//如果是第一次进入游戏
        {
            O1HD.getServices().userSrv.setInfinity(0);
            // O1HD.getModules().uiControlModule.showInfinityPower(gameConfig.FIRST_PLAY_INFINITY_PROBABILITY, mainRoot.UIRoot);
        }

        let unlockLevelTime:number = O1HD.getServices().userSrv.getLevelAllUnlockTime();
        if(Date.now() - unlockLevelTime > 60*60*24*1000){
            O1HD.getServices().userSrv.setLevelAllUnlockTime();
            O1HD.getServices().userSrv.setLevelAllUnlockFlag(false);
            UnLockPage.unlockAllTime = 0;
            O1HD.emit("refrashAll");
        }

    }

    onBtnAddPower() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        modules().uiControlModule.showUserPropGet(REWARD_TYPE.POWER, gameConfig.ADD_ONCE_POWER);
        // modules().uiControlModule.showUserPropGet(REWARD_TYPE.POWER, gameConfig.ADD_ONCE_POWER, null, () => {
        //     O1HD.getModules().uiControlModule.showInfinityPower(gameConfig.CLICK_POWER_INFINITY_PROBABILITY, mainRoot.UIRoot);
        // });
    }


    update(dt) {
    }
}
